package com.atguigu.schedule.controller;

import com.atguigu.schedule.pojo.SysUser;
import com.atguigu.schedule.service.SysUserService;
import com.atguigu.schedule.service.impl.SysUserServiceImpl;
import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;

import java.io.IOException;

@WebServlet("/User")
public class SysUserController extends BaseController {

    private SysUserService userService=new SysUserServiceImpl();
   /*
   * 接受用户注册请求时的业务处理方法（接口 不是java中的interface）
   * */
    protected void regist(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //1 接受客户端提交的参数
        String username = req.getParameter("username");
        String userPwd = req.getParameter("userPwd");
        //2 调用服务层方法，完成注册功能
        //将参数放入一个SysUser对象中，在调用regist方法时传入
        SysUser sysUser=new SysUser(null,username,userPwd);

        int rows=userService.regist(sysUser);
        if(rows>0){
            resp.sendRedirect("/registSuccess.html");
        }else{
            resp.sendRedirect("/registFail.html");
        }

        //3 根据注册结果（成功 失败 ）做页面跳转


    }

    /* @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //判断此次请求的是 增 删 改 查？

       *//* String requestURI = req.getRequestURI();  //schedule/add
        String[] split = requestURI.split("/");
        String methodName = split[split.length - 1];*//*
       *//* if (methodName.equals("add")) {
            //做增加处理
            add(req, resp);
        } else if (methodName.equals("find")) {
            //做查询处理
            find(req, resp);
        } else if (methodName.equals("update")) {
            //做修改处理
            update(req, resp);
        } else if (methodName.equals("remove")) {
            //做删除处理
            remove(req, resp);
        }*//*
        //使用反射通过方法名调用对应的方法
        Class aClass = this.getClass();
        //获取方法
        try {
            Method declaredMethod = aClass.getDeclaredMethod(methodName, HttpServletRequest.class, HttpServletResponse.class);
            //暴力破解方法的访问修饰符的限制
            declaredMethod.setAccessible(true);
            //执行方法
            declaredMethod.invoke(this, req, resp);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }*/

}
